import React, { Component } from 'react'
import QueryList from '@/components/basicWidget/QueryList'
import { message, Modal } from 'antd'
import { getBlob, post } from '@/utils/requestUtil'
import { TableButton } from '@/components/basicWidget/Button'

const { confirm } = Modal
/**
 * 附件管理页面
 * @author liu_jshan
 * @date 2024-03-01
 */
class FileManage extends Component {

    constructor(props) {
        super(props)
        this.state = {
        }
    }

    async componentDidMount() {

    }

    openView = (path) => {
        return async () => {
            const blob = await getBlob(path);
            const viewURL = URL.createObjectURL(blob);
            window.open(viewURL);
            URL.revokeObjectURL(viewURL);
        }
    }

    actionColumns = (columns, data) => {
        columns.push({ title: '查看', dataIndex: 'check' })
        data = data.map(item => {
            item.check = (
                <TableButton onClick={this.openView(item.uploadPath)}>查看</TableButton>
            )
            return item
        })
        return { newColumns: columns, newData: data }
    }

    deleteFiles = async () => {
        const { selectedRows } = this.queryList.state
        if (selectedRows.length === 0) {
            message.warning('请选择要删除的数据')
            return false
        }
        let haveUse = false // 是否存在生效的文件
        const deletePaths = []
        selectedRows.forEach(item => {
            if (item.state === '有效') {
                haveUse = true
            }
            deletePaths.push(item.uploadPath)
        })
        if (haveUse === true) {
            confirm({
                content: '选择的文件中存在有效的文件，确定要删除吗',
                onOk: async () => {
                    const response = await post('/file/delete/files', { body: JSON.stringify(deletePaths) })
                    if (response) {
                        message.success('删除成功')
                        this.queryList.onQuery()
                    }
                },
                onCancle: async () => {
                    return
                }
            })
        } else {
            const response = await post('/file/delete/files', { body: JSON.stringify(deletePaths) })
            if (response) {
                message.success('删除成功')
                this.queryList.onQuery()
            }
        }
    }

    render() {
        return (
            <QueryList
                queryCode='fileManage'
                actionColumns={this.actionColumns}
                deleteFiles={this.deleteFiles}
                ref={queryList => this.queryList = queryList}
            >
            </QueryList>
        )
    }
}

export default FileManage
